﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using TSharp.Core.Aop.Handler;

namespace TSharp.Core.Aop.Attribute
{
    /// <summary>
    /// 日志的定制特性
    /// </summary>
    public class LogAttribute : HandlerAttribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LogAttribute"/> class.
        /// </summary>
        /// <param name="messageTemplate">The message template.</param>
        /// <param name="categoryName">Name of the policy.</param>
        public LogAttribute(string messageTemplate, string categoryName)
        {
            MessageTemplate = messageTemplate;
            CategoryName = categoryName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LogAttribute"/> class.
        /// </summary>
        /// <param name="categoryName">Name of the policy.</param>
        public LogAttribute(string categoryName)
            : this(string.Empty, categoryName)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LogAttribute"/> class.
        /// </summary>
        public LogAttribute()
            : this(string.Empty, GolbalCategoryName)
        {
            Order = 4;
        }

        /// <summary>
        /// 全局日志配置名称
        /// </summary>
        public static string GolbalCategoryName { get; set; }

        /// <summary>
        /// 日志消息模板
        /// </summary>
        public string MessageTemplate { get; set; }

        /// <summary>
        /// Policy名称
        /// </summary>
        public string CategoryName { get; set; }

        /// <summary>
        /// Derived classes implement this method. When called, it
        /// creates a new call handler as specified in the attribute
        /// configuration.
        /// </summary>
        /// <param name="container">The <see cref="T:Microsoft.Practices.Unity.IUnityContainer"/> to use when creating handlers,
        /// if necessary.</param>
        /// <returns>A new call handler object.</returns>
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new LogHandler(MessageTemplate, CategoryName, Order);
        }
    }
}